//
//  SecondViewController.m
//  省市区
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "Province.h"
#import "City.h"
#import "OneVCOfCity.h"
#import "OneVCOfZone.h"

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"省、市->区";
    self.navigationController.navigationBar.barTintColor = [UIColor cyanColor];
    self.dataArr = [self handleData].mutableCopy;
    [self createTableView];
}

- (NSArray *)handleData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"txt"];
    NSString *datastring = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arrOfPlace = [datastring componentsSeparatedByString:@"\n"];
    //创建一个省数组
    NSMutableArray *proArr = [NSMutableArray array];
    for (NSString *name in arrOfPlace) {
        if (![name hasPrefix:@" "]) {
            //建立省字典
            NSMutableDictionary *dicOfPro = [NSMutableDictionary dictionary];
            [dicOfPro setObject:name forKey:@"proName"];
            //建立市数组
            NSMutableArray *cityArr = [NSMutableArray array];
            [dicOfPro setObject:cityArr forKey:@"cityArr"];
            [proArr addObject:dicOfPro];
            //NSLog(@"%@",dicOfPro);
        }
        else if ([name hasPrefix:@"  "] && ![name hasPrefix:@"    "]) {
            //建立市字典
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setObject:name forKey:@"cityName"];
            //建立区数组
            NSMutableArray *areaArr = [NSMutableArray array];
            [dicOfCity setObject:areaArr forKey:@"areaArr"];
            NSMutableDictionary *cityDic = [proArr lastObject];
            NSMutableArray *marrCity = cityDic[@"cityArr"];
            [marrCity addObject:dicOfCity];
            //NSLog(@"111%@",dicOfCity);
        }else {
            NSMutableDictionary *proDic = [proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *areaArr = cityDic[@"areaArr"];
            [areaArr addObject:name];
            
        }
    }
    //2.省数组
    /** 2. 将数组字典的处理方式转换成数组模型(Class) */
    // 省数组 - Province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in arrOfPlace) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.marr addObject:city];
        } else {
            
            City *city = [[[allProArray lastObject] marr] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.marr addObject:zone];
        }
    }
    return allProArray;
}

- (void)createTableView
{
    _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
}

#pragma mark - table DataSource delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[self.dataArr objectAtIndex:section] marr] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.textLabel.text = [[_dataArr[indexPath.section] marr][indexPath.row]name];    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OneVCOfZone *vc = [[OneVCOfZone alloc]init];
    
    vc.dataArr = [[_dataArr[indexPath.section] marr][indexPath.row]marr];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_dataArr[section] name];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
